<?php
/**
 * Created by: zhaoyi (834574377@qq.com)
 * Time: 2023/1/9 17:06
 * Blog: https://www.yuque.com/huangzhenlian
 */

declare(strict_types=1);

namespace App\Application\Api\Task;


use App\Annotation\Api;
use App\Exception\ErrorException;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\DbConnection\Db;
use Hyperf\AsyncQueue\Annotation\AsyncQueueMessage;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;

#[Controller("/api/OrdercleanerTask")]
#[Crontab(rule: "*\/5 * * * * *", name: "保洁订单", callback: "ordercleaner", memo: "保洁订单定时发送", enable: "isordercleaner")]
class OrdercleanerTask
{
    //订单状态0未开始，1进行中，2已结束，3已退款，4已取消，5已分账，6待付款
    //订单分账定时更新
    //https://zly.qile168.net/api/OrdercleanerTask/ordercleaner
    #[Api]
    #[GetMapping("ordercleaner")]
    public function ordercleaner()
    {
        $where = [];
        $where[] = ['end_time', '<', time()];
        $where[] = ['cleaner_status', '=', 3];
        $res=   Db::table('order')->where($where)->whereIn('status', ['2', '3', '4', '5'])->update([
            'cleaner_status' => 0,
            'updated_at' => date('Y-m-d H:i:s', time())
        ]);
        if ($res) {
            return true;
        } else {
            return false;
        }

        //发送保洁消息

//        $office_service = new OfficeService();
//        $office_service = new O();
//        $open_id = 'open_id';
//        $template_id = 'template_id';
//        $data = ['a' => 'x', 'd' => time()];
//        $url = 'url';
//        $mini_program_appid = 'mini_program_appid';
//        $res = $office_service->template()
//            ->sendTemplateMsg($open_id, $template_id, $data, $url, $mini_program_appid);

    }
    /*
      *
      *订单结束，带发送保洁的订单
      * */
    public function isordercleaner(): bool
    {
        $where = [];
        $where[] = ['end_time', '<', time()];
        $where[] = ['cleaner_status', '=', 3];
        $order = Db::table('order')->where($where)->whereIn('status', ['2', '3', '5'])->select('*')->first();
        if (!empty($order)) {
            return true;
        } else {
            return false;
        }
    }



}